from datetime import datetime
from .base import BaseModel
from ..extensions.extensions import db

class Permission(BaseModel):
    """权限表"""
    __tablename__ = 'permissions'
    
    permission_name = db.Column(db.String(100), nullable=False, comment='权限名称')
    permission_code = db.Column(db.String(50), nullable=False, unique=True, comment='权限代码')
    description = db.Column(db.String(200), comment='权限描述')
    resource_type = db.Column(db.String(50), nullable=False, comment='资源类型(API,MENU,BUTTON等)')
    resource_path = db.Column(db.String(200), comment='资源路径')
    parent_id = db.Column(db.Integer, comment='父权限ID')
    sort_order = db.Column(db.Integer, default=0, comment='排序号')
    status = db.Column(db.Boolean, nullable=False, default=True, comment='状态(1:启用,0:禁用)')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'permission_name': self.permission_name,
            'permission_code': self.permission_code,
            'description': self.description,
            'resource_type': self.resource_type,
            'resource_path': self.resource_path,
            'parent_id': self.parent_id,
            'sort_order': self.sort_order,
            'status': self.status,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        } 